package example.hellosecurity.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.crypto.password.PasswordEncoder;

/**
 * create by: Beer Bear
 * description: 当开启该配置类时并重写①方法时就覆盖了默认的user + security password
 * ①方法configure (AuthenticationManagerBuilder auth)方法
 *
 * 注意：新版的SpringSecurity是不能不加加密协议的。Assert.notNull(passwordEncoder, "passwordEncoder cannot be null");
 *
 * create time: 2021/9/13 21:14
 */
@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {

    @Autowired
    private PasswordEncoder BCryptPassword;

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        // 基于内存的用户存储
        auth.inMemoryAuthentication()
                .passwordEncoder(BCryptPassword)
                .withUser("yaodao")
                    .password(BCryptPassword.encode("yaodao"))
                    .authorities("ROLE_USER")
                .and()
                .withUser("beerbear")
                    .password(BCryptPassword.encode("beerbear"))
                    .authorities("ROLE_USER");
    }
}
